package com.itxx.treeSet.domain;

import java.text.Collator;
import java.util.Comparator;
import java.util.Locale;
import java.util.TreeSet;

public class TeacherTest1 {
    public static void main(String[] args) {
        //创建TreeSet,通过比较器排序 直接将重写方法作为参数传递TreeSet的构造方法中.
        TreeSet<Teacher> teachers = new TreeSet<>(new Comparator<Teacher>() {
            @Override
            public int compare(Teacher o1, Teacher o2) {
                //主要条件  总成绩
                int result = (o1.getChinese() + o1.getMath()) - (o2.getChinese() + o2.getMath());
                //次要条件 语文成绩
                if (result == 0) {
                    result = o1.getChinese() - o2.getChinese();
                }
                //次要条件 数学成绩
                if (result == 0) {
                    result = o1.getMath() - o2.getMath();
                }
                //次要条件 年龄
                result = result == 0 ? o1.getAge() - o2.getAge() : result;
                //次要条件 姓名首字母
                if (result == 0) {
                    Collator instance = Collator.getInstance(Locale.CHINA);
                    result = instance.compare(o1.getName(), o2.getName());
                }
                return result;
            }
        });
        //添加元素
        teachers.add(new Teacher("张三", 23, 88, 99));
        teachers.add(new Teacher("李三", 23, 88, 99));
        teachers.add(new Teacher("阿三", 23, 88, 99));
        teachers.add(new Teacher("白三", 46, 88, 99));
        teachers.add(new Teacher("常三", 23, 46, 75));
        teachers.add(new Teacher("杜三", 52, 83, 89));
        teachers.add(new Teacher("范三", 23, 83, 99));
        for (Teacher teacher : teachers) {
            System.out.println(teacher);
        }
    }
}
